<template>
	<view class="container">
		<view class="page-header">
			<text class="page-title">投诉建议</text>
		</view>

		<view class="form-container">
			<view class="form-item">
				<text class="form-label">投诉类型</text>
				<view class="type-selector">
					<view class="type-item" v-for="(item, index) in complaintTypes" :key="index"
						:class="{'selected': selectedType === index}" @click="selectType(index)">
						<text>{{item}}</text>
					</view>
				</view>
			</view>

			<view class="form-item">
				<text class="form-label">问题描述</text>
				<textarea class="complaint-input" placeholder="请详细描述您遇到的问题或建议（10-500字）" v-model="description"
					maxlength="500"></textarea>
				<text class="word-count">{{description.length}}/500</text>
			</view>

			<view class="form-item">
				<text class="form-label">上传图片（选填）</text>
				<view class="image-uploader">
					<view class="image-item" v-for="(item, index) in images" :key="index">
						<image class="uploaded-image" :src="item" mode="aspectFill"></image>
						<text class="delete-icon" @click="deleteImage(index)">×</text>
					</view>
					<view class="upload-button" @click="chooseImage" v-if="images.length < 3">
						<text class="plus-icon">+</text>
					</view>
				</view>
				<text class="upload-tip">最多上传3张图片</text>
			</view>

			<view class="form-item">
				<text class="form-label">联系方式</text>
				<input class="contact-input" placeholder="请输入您的手机号码" v-model="contactPhone" />
			</view>
		</view>

		<button class="submit-btn" @click="submitComplaint">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				complaintTypes: ['服务问题', '系统故障', '收费问题', '医疗质量', '其他建议'],
				selectedType: -1,
				description: '',
				images: [],
				contactPhone: ''
			}
		},
		onLoad() {
			// 自动填充用户手机号
			this.contactPhone = uni.getStorageSync('telephone') || '';
		},
		methods: {
			selectType(index) {
				this.selectedType = index;
			},
			chooseImage() {
				uni.chooseImage({
					count: 3 - this.images.length,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.images = [...this.images, ...res.tempFilePaths];
					}
				});
			},
			deleteImage(index) {
				this.images.splice(index, 1);
			},
			submitComplaint() {
				// 表单验证
				if (this.selectedType === -1) {
					this.showToast('请选择投诉类型');
					return;
				}

				if (this.description.length < 10) {
					this.showToast('问题描述至少10个字');
					return;
				}

				if (!this.contactPhone) {
					this.showToast('请输入联系方式');
					return;
				}

				// 模拟提交
				uni.showLoading({
					title: '提交中...'
				});

				setTimeout(() => {
					uni.hideLoading();
					uni.showToast({
						title: '提交成功',
						icon: 'success'
					});

					// 重置表单
					this.selectedType = -1;
					this.description = '';
					this.images = [];

					// 2秒后返回
					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
				}, 1500);
			},
			showToast(message) {
				uni.showToast({
					title: message,
					icon: 'none'
				});
			},
			viewHistory() {
				uni.navigateTo({
					url: '/pages/mine/complaints/history'
				});
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		padding: 30rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.page-header {
		padding: 20rpx 0 40rpx;
	}

	.page-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	.form-container {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
	}

	.form-item {
		margin-bottom: 30rpx;
	}

	.form-label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 20rpx;
		font-weight: 500;
	}

	.type-selector {
		display: flex;
		flex-wrap: wrap;
	}

	.type-item {
		padding: 15rpx 25rpx;
		background-color: #f7f7f7;
		border-radius: 30rpx;
		margin-right: 20rpx;
		margin-bottom: 20rpx;
		font-size: 26rpx;
		color: #666;
	}

	.type-item.selected {
		background-color: #e1f1ff;
		color: #0086f6;
	}

	.complaint-input {
		width: 100%;
		height: 240rpx;
		background-color: #f7f7f7;
		border-radius: 10rpx;
		padding: 20rpx;
		font-size: 26rpx;
		box-sizing: border-box;
	}

	.word-count {
		text-align: right;
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.image-uploader {
		display: flex;
		flex-wrap: wrap;
	}

	.image-item {
		width: 180rpx;
		height: 180rpx;
		margin-right: 20rpx;
		margin-bottom: 20rpx;
		position: relative;
	}

	.uploaded-image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.delete-icon {
		position: absolute;
		top: -20rpx;
		right: -20rpx;
		width: 40rpx;
		height: 40rpx;
		background-color: rgba(0, 0, 0, 0.5);
		color: #fff;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
	}

	.upload-button {
		width: 180rpx;
		height: 180rpx;
		background-color: #f7f7f7;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 20rpx;
	}

	.plus-icon {
		font-size: 60rpx;
		color: #999;
	}

	.upload-tip {
		font-size: 24rpx;
		color: #999;
	}

	.contact-input {
		width: 100%;
		height: 80rpx;
		background-color: #f7f7f7;
		border-radius: 10rpx;
		padding: 0 20rpx;
		font-size: 26rpx;
		box-sizing: border-box;
	}

	.submit-btn {
		width: 100%;
		height: 90rpx;
		background-color: #0086f6;
		color: #fff;
		border-radius: 45rpx;
		font-size: 30rpx;
		margin-bottom: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.history-section {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.history-text {
		font-size: 28rpx;
		color: #333;
	}

	.arrow {
		font-size: 36rpx;
		color: #999;
	}
</style>